<?php

namespace app\health\controller;

use app\health\model\HealthWebsiteVideo;
use think\exception\DbException;

class Video extends ApiBase
{
    protected $noNeedLogin = ['lists','detail'];

    /**
     * @return void
     * @throws DbException
     */
    public function lists(): void
    {
        $type     = $this->request->param('type', 1);
        $limit    = $this->request->param('limit', 10);
        $paginate = HealthWebsiteVideo::where('type', $type)
            ->field('id,name,cover,duration')
            ->paginate($limit);
        $this->success('success', [
            'total' => $paginate->total(),
            'lists' => $paginate->items()
        ]);
    }

    /**
     * 视频详情
     * @return void
     * @throws DbException
     */
    public function detail(): void
    {
        $id    = $this->request->param('id/d', 0);
        $video = HealthWebsiteVideo::get($id);
        if (!$video) {
            $this->error('视频不存在');
        }
        $this->success('success', [
            'name'          => $video['name'],
            'cover'         => $video['cover'],
            'duration'      => $video['duration'],
            'duration_text' => $video['duration_text'],
            'url'           => $video['url'],
        ]);
    }
}